package com.victor.rxjavademo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.orhanobut.logger.Logger;

import rx.Observable;
import rx.Observer;
import rx.Subscriber;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //        观察者
        Observer<String> observer = new Observer<String>() {

            @Override
            public void onNext(String value) {
                Logger.e("Observer onNext: " + value);
            }

            @Override
            public void onError(Throwable e) {
                Logger.e("Observer onError");
            }

            @Override
            public void onCompleted() {
                Logger.e("Observer onComplete");
            }
        };

        //        订阅者
        Subscriber<String> subscriber = new Subscriber<String>() {

            @Override
            public void onNext(String s) {
                Logger.e("subscriber onNext: " + s);
            }

            @Override
            public void onError(Throwable t) {
                Logger.e("subscriber onError");
            }

            @Override
            public void onCompleted() {
                Logger.e("subscriber onComplete");
            }
        };

        // 被观察者
        Observable<String> observable = Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                subscriber.onNext("hello");
                subscriber.onNext("aaa");
                subscriber.onNext("bbb");
                subscriber.onCompleted();
            }
        });
        //        observable.subscribe(subscriber);//订阅

//        String[] names = {"aaa", "bbb", "cccc"};
//        Observable.from(names).subscribe(new Action1<String>() {
//            @Override
//            public void call(String s) {
//                Logger.e(s);
//            }
//        });


    }
}